package net.mypapit.mobile.BatteryStat;


import android.os.BatteryManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class BatteryStat extends Activity {
	ProgressBar pb;
	
	TextView tvStatus, tvLevel, tvTemp,tvHealth;
	
	
	//innerclass
		 private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
			 		public void onReceive(Context context, Intent intent) {
			 			int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
			 			int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
			 			int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
			 			int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
			 			int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
			 			int volt= intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
			 			
			 			
			 			
			 			int percent = (level*100)/scale;
			 			
			 			pb.setProgress(percent);
			 			tvTemp.setText( temp/10+ " \u2103 / " + volt +" mV");
			 			
			 			StringBuffer sbstatus = new StringBuffer("");
			 			StringBuffer sbhealth = new StringBuffer("");
			 			
			 			switch(status){
			 				case BatteryManager.BATTERY_STATUS_CHARGING:
			 					sbstatus.append("Charging");
			 				break;
			 				case BatteryManager.BATTERY_STATUS_DISCHARGING:
			 					sbstatus.append("Discharging");
			 				break;
			 				case BatteryManager.BATTERY_STATUS_FULL:
			 					sbstatus.append("Fully charged");
			 				break;
			 				
			 				case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
			 					sbstatus.append("Not Charging");
			 				break;
			 				
			 				default:
			 					sbstatus.append("Unknown status");
			 				
			 			
			 			}
			 			
			 			switch (health){
			 			case BatteryManager.BATTERY_HEALTH_COLD:
			 				sbhealth.append("Battery is Cold");
			 			break;
			 			
			 			case BatteryManager.BATTERY_HEALTH_GOOD:
			 				sbhealth.append("Good Condition");
			 			break;
			 			
			 			case BatteryManager.BATTERY_HEALTH_DEAD:
			 				sbhealth.append("Dead Battery- replace immediately");
			 			break;
			 			
			 			case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
			 				sbhealth.append("Overvoltage - please disconnect power source");
			 			break;
			 			
			 			case BatteryManager.BATTERY_HEALTH_UNKNOWN:
			 				sbhealth.append("Unknown Condition");
			 			break;
			 			
			 			case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
			 				sbhealth.append("Fail to detect battery condition");
			 			break;
			 				
			 			
			 			}
			 			
			 			tvHealth.setText(sbhealth.toString());
			 			tvStatus.setText(sbstatus.toString());
			 			tvLevel.setText("Battery level: " + percent +" %");
			 			
			 			
			 	}

					
		 }; // end of inner class
		 
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_battery_stat);
		
		pb = (ProgressBar) findViewById(R.id.progressBar1);
		tvTemp = (TextView) findViewById(R.id.textViewTemp);
		tvStatus = (TextView) findViewById(R.id.textViewStatus);
		tvLevel = (TextView) findViewById(R.id.textViewLevel);
		tvHealth = (TextView) findViewById(R.id.textViewHealth);

		
	this.registerReceiver(batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
		
		
		
		
		
	}
	protected void onStop()
	{
	    unregisterReceiver(batteryInfoReceiver);
	    //unregisterReceiver(deliveryBroadcastReceiver);
	    super.onStop();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.battery_stat, menu);
		return true;
	}
	
	
    public boolean onOptionsItemSelected(MenuItem item){
    	switch(item.getItemId()) {
    	case R.id.action_about:
    		// do something
    		try {
    		showDialog();
    		} catch (NameNotFoundException ex){
    			Toast toast = Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT);
    			toast.show();
    			
    		
    		}
    		
    	}
    	return true;
    }
    private void showDialog() throws NameNotFoundException {
		// TODO Auto-generated method stub
    	final Dialog dialog = new Dialog(this);
    	dialog.setContentView(R.layout.about_dialog);
    	dialog.setTitle("About My Battery Stats "+ getPackageManager().getPackageInfo(getPackageName(), 0).versionName);
    	dialog.setCancelable(true);
    	
    	//text
    	TextView text = (TextView) dialog.findViewById(R.id.tvAbout);
    	text.setText(R.string.txtLicense);
    	
    	//icon image
    	ImageView img = (ImageView) dialog.findViewById(R.id.ivAbout);
    	img.setImageResource(R.drawable.ic_launcher);
    	
    	
    	
    	dialog.show();
    	
    	
    	
    	
		
	}
    

}
